package com.yeqiangwei.tools;

import java.io.UnsupportedEncodingException;


@SuppressWarnings("unused")
public class Gb {
    public String gb(String txt){
        String rTxt = new String("");
        if(txt!=null && !txt.equals("")){
            //try{
                //rTxt = new String(txt.getBytes("ISO-8859-1"),"UTF-8");//编码转化
                rTxt = txt;
            //}catch (UnsupportedEncodingException e) {

            //}
        }else{
            rTxt = "";
        }
        return rTxt;
    }
    
    public String iso_utf(String txt){
        String rTxt = new String("");
        if(txt!=null && !txt.equals("")){
            try{
               rTxt = new String(txt.getBytes("UTF-8"),"ISO-8859-1");//编码转化
                rTxt = txt;
            }catch (UnsupportedEncodingException e) {

            }
        }else{
            rTxt = "";
        }
        return rTxt;
    }
    
    public String toUTF16BE(String txt){
        String rTxt = new String("");
        if(txt!=null && !txt.equals("")){
            try{
               rTxt = new String(txt.getBytes("UTF-8"),"UTF-16BE");//编码转化
               rTxt = txt;
               
            }catch (UnsupportedEncodingException e) {
            	e.printStackTrace();
            }
        }else{
            rTxt = "";
        }
        return rTxt;
    }
    
    public static String encodeUrl(String s){//网页URL下载地址编码
        if(s==null || s.equals("")){
            return "";
        }
        StringBuffer sb = new StringBuffer(); 
        for (int i=0;i<s.length();i++) { 
            char c = s.charAt(i); 
            if (c >= 0 && c <= 255) { 
                sb.append(c); 
            }else{ 
		         byte[] b; 
		         try { 
		             b = Character.toString(c).getBytes("UTF-8"); 
		         } catch (Exception ex) { 
		             System.out.println(ex); 
		             b = new byte[0]; 
		         } 
		         for (int j = 0; j < b.length; j++) { 
		             int k = b[j]; 
		             if (k < 0) k += 256; 
		             sb.append("%" + Integer.toHexString(k). 
		             toUpperCase()); 
	             } 
	        } 
	    } 
        return sb.toString(); 
    } 

    public static String toUTF8(String s){
        if(s==null) return "";
        String rTxt = new String("");
        try {
            rTxt = new String(s.getBytes("ISO-8859-1"),"UTF-8");//编码转化
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            rTxt = s;
        }
 
       return rTxt;
    }
    
    public static void  main(String args[]){
     Gb g = new Gb();
    	 String s = g.toUTF8("你怎么做到的");
    	 System.out.println(s);
    }
}
